<?php
/**
 * Description of DetalhesIncludeViewGenerator
 *
 * @author Magno
 */
class DetalhesIncludeViewGenerator extends ViewGenerator{

    public function __construct($tableMeta, $viewParams) {
        parent::__construct($tableMeta, $viewParams, Constantes::$TEMPLATES_VIEW_DIR."detalhesInclude.bloum");
    }

    public function generate() {
        parent::generateNames();        
        $this->generateAttValues();
        return $this->templateView;
    }

    public function generateAttValues(){

        $tableName = $this->tableMeta->getName();
        $attributes = $this->tableMeta->getAttributes();
        $attrsKeys = array_keys($attributes);

        $attDetalhe = "";
        $fkDetalhe = "";

        $fechouTagTr = false;

        for ($i = 0; $i < count($attrsKeys); $i++) {
            $att = $attributes[$attrsKeys[$i]];
            $name = $att->getName();

            if($att->isPrimaryKey() && !$att->isForeignKey())  continue;

            if(!$att->isForeignKey()){

                if(strlen($attDetalhe) <= 0)
                    $attDetalhe .= "<tr>\n\t\t\t";

                $attDetalhe .= '<td class="tituloDetalhe">'.ucfirst($name).':</td>'."\n\t\t".'<td class="valorDetalhe">{$'.$tableName.'_'.$name.'|default:\'\'}</td>'."\n\t\t";
                $fechouTagTr = false;
                if($i % 2 == 0){
                    $attDetalhe .= "</tr>\n\t<tr>\n\t\t";
                    $fechouTagTr = true;
                }
            }
            else{
                $tbFkName = str_replace("ID", "", $name);
                $fks = $this->tableMeta->getFKReferences();
                $fkMeta = null;
                foreach ($fks as $fk) {
                    foreach ($fk->getAttributes() as $fkAtt) {
                        if(strcmp(strtolower($fkAtt->getName()), strtolower($tbFkName."ID")) == 0){
                            $fkMeta = $fk;
                            break;
                        }
                    }
                    if($fkMeta != null) break;
                }
                if($fkMeta == null)
                    throw new Exception ("Tabela (".$tbFkName.") Referenciada Na Herança com (".$this->tableMeta->getName().") Não Encontrada!!!");

                $tbReference = $fkMeta->getTableReference()->getName();
                $tab = "\n";
                if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_COMBO) == 0){

                    $comboDisplay = "";
                    foreach ($fk->getTableReference()->getAttributes() as $attFk) {
                        if($attFk->isComboDisplay()){
                            $comboDisplay = ucfirst($attFk->getName());
                            break;
                        }
                    }

                    if(strlen($attDetalhe) <= 0)
                        $attDetalhe .= "<tr>\n\t\t\t";

                    $attDetalhe .= '<td class="tituloDetalhe">'.ucfirst($tbReference).':</td>'."\n\t\t".'<td class="valorDetalhe">{$'.$tableName.'_'.$tbReference.'->get'.$comboDisplay.'()|default:\'\'}</td>'."\n\t\t";
                    $fechouTagTr = false;

                    if($i % 2 == 0){
                        $attDetalhe .= "</tr>\n\t<tr>\n\t\t";
                        $fechouTagTr = true;
                    }

                }else if(strcmp($fkMeta->getTypeForm(), Constantes::$TYPE_FORM_INCLUDE) == 0){
                    $fkDetalhe .= '{include file="view/'.$tbReference.'/detalhesInclude.tpl"}'."\n\t\t";
                }
            }
        }

        if(!$fechouTagTr)
            $attDetalhe .= "</tr>";

        $this->templateView = str_replace(Constantes::$TPL_VIEW_DETALHES_ATT_DETALHE, $attDetalhe, $this->templateView);
        $this->templateView = str_replace(Constantes::$TPL_VIEW_DETALHES_FK_DETALHE, $fkDetalhe, $this->templateView);

    }

    public function generateFile($generate = true){
        if($generate)
            $this->generate();
        parent::generateFile("detalhesInclude");
    }
}
?>
